package cn.edu.zjut.chopper.registry.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.edu.zjut.chopper.constant.Constant;
import cn.edu.zjut.chopper.message.RemoteInterface;
import cn.edu.zjut.chopper.name.NameServer;
import cn.edu.zjut.chopper.registry.RegistryContext;

/**
 * Servlet implementation class RemoteInterfaceServerView
 */
public class RemoteInterfaceServerView extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Log LOG = LogFactory.getLog(RemoteInterfaceServerView.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RemoteInterfaceServerView() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		NameServer nameServer = (NameServer) RegistryContext
				.get(Constant.NameServer);
		if (nameServer == null) {
			getServletContext().getRequestDispatcher("/error.jsp").forward(
					request, response);
			if (LOG.isErrorEnabled()) {
				LOG.error("can not get NameServer Instance.");
			}
			return;
		}
		Map<String, Set<RemoteInterface>> remoteInterfaces = nameServer.list();
		Map<String, Set<RemoteInterface>> serverRemoteInterfaces = new HashMap<String, Set<RemoteInterface>>();
		for (Entry<String, Set<RemoteInterface>> riSetEntry : remoteInterfaces
				.entrySet()) {
			for (RemoteInterface ri : riSetEntry.getValue()) {
				String serverIP = getRemoteInterfaceIP(ri);
				Set<RemoteInterface> riSet = serverRemoteInterfaces
						.get(serverIP);
				if (riSet == null) {
					Set<RemoteInterface> newriSet = new HashSet<RemoteInterface>();
					newriSet.add(ri);
					serverRemoteInterfaces.put(serverIP, newriSet);
				} else {
					riSet.add(ri);
				}
			}
		}
		request.setAttribute("serverRemoteInterfaces", serverRemoteInterfaces);
		getServletContext().getRequestDispatcher(
				"/remote_interface_server_view.jsp").forward(request, response);
	}

	private String getRemoteInterfaceIP(RemoteInterface ri) {
		if (ri == null || ri.getUrl() == null) {
			return null;
		}
		String serverIP = ri.getUrl().substring(ri.getUrl().indexOf("//") + 2,
				ri.getUrl().indexOf("/", 7));
		return serverIP;
	}

}
